package com.chinasoft.obj;

import com.chinasoft.enumdef.Direction;
import com.chinasoft.enumdef.Elem;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@EqualsAndHashCode
public class BoxGroup implements CommonAble {

    List<Box> boxes = new ArrayList<>();
    int count;
    MapData data;

    public BoxGroup(MapData data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "BoxGroup{" +
                "boxes=" + boxes +
                ", count=" + count +
                '}';
    }

    public void init(Man man, Direction dr) {
        this.clear();
        Box box1 = man.getNextBox(dr);
        boxes.add(box1);
        Box b = box1;
        while((b = b.getNextBox(dr)) != null) {
            boxes.add(b);
        }
        this.count = boxes.size();
        System.out.println(this);
    }

    private void clear() {
        boxes.clear();
        count = 0;
    }

    @Override
    public void move(Direction dr) {
        for(int i = boxes.size() - 1; i >= 0; i--) {
            boxes.get(i).move(dr);
        }
    }

    @Override
    public Elem getNextObj(Direction dr) {
        int lastIndex = this.boxes.size() - 1;
        Box last = this.boxes.get(lastIndex);
        Elem obj = last.getNextObj(dr);
        return obj;
    }

    @Override
    public Box getNextBox(Direction dr) {
        // null 只是支持兼容性
        return null;
    }

    @Override
    public boolean canMove(Direction dr) {
        Elem obj = getNextObj(dr);
        if (obj == Elem.Glass || obj == Elem.Gold) {
            return true;
        } else {
            return false;
        }
    }
}
